package sdu.icat.ltp.persistant;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.bson.Document;
import org.bson.conversions.Bson;

import com.mongodb.client.MongoCursor;
import com.mongodb.client.model.Filters;

import sdu.icat.ltp.api.TransElement;
import sdu.icat.ltp.mongo.MongoDbConnection;
import sdu.icat.ltp.mongo.MongoDbFascade;
import sdu.icat.ltp.tool.ArticleConfig;

/**
 * mongodb 持久化
 * @author ljh_2015
 *
 */
public class MongoPersistant implements AbstractPersistant{

    private final Logger log = Logger.getLogger(MongoPersistant.class);
    MongoDbConnection conn;

    /* (non-Javadoc)
     * @see sdu.icat.ltp.persistant.AbstractPersistant#persistant(sdu.icat.ltp.api.TransElement, java.lang.String[])
     */
    @Override
    public void persistant(TransElement element) {
	if(!"xml".equals(element.getFormat())) {
	    log.error("仅支持xml数据格式的传输");
	    return;
	} 
	if(conn==null) {
	    conn = MongoDbFascade.createConnection();
	}
	conn.insertText(element);
    }

    
    /* (non-Javadoc)
     * @see sdu.icat.ltp.persistant.AbstractPersistant#listView()
     */
    @Override
    public List<Document> listView() {
	if(conn==null) {
	    conn = MongoDbFascade.createConnection();
	}
	MongoCursor<Document> cursor = conn.getCollection("document")
		.find(Filters.eq("title", "simple"))
		.iterator();
	List<Document> document = new ArrayList<>();
	while(cursor.hasNext()) {
	    Document doc = cursor.next();
	    document.add(doc);
	}
	return document;
    }

    @Override
    public Document findDocument(String text) {
	if(conn==null) {
	    conn = MongoDbFascade.createConnection();
	}
	return conn.getCollection("document")
		.find(Filters.eq("text", text)).first();
    }
    
    @Override
    public List<Document> findDocumentWithTitle(String title) {
	if(conn==null) {
	    conn = MongoDbFascade.createConnection();
	}
	MongoCursor<Document> cursor = conn.getCollection("document")
		.find(Filters.eq("title", title))
		.iterator();
	List<Document> document = new ArrayList<>();
	while(cursor.hasNext()) {
	    Document doc = cursor.next();
	    document.add(doc);
	}
	return document;
    }

    @Override
    public List<Document> findDocuments(String regex) {
	if(conn==null) {
	    conn = MongoDbFascade.createConnection();
	}
	MongoCursor<Document> cursor = conn.getCollection("document")
		.find(Filters.regex("text", regex))
		.iterator();
	List<Document> document = new ArrayList<>();
	while(cursor.hasNext()) {
	    Document doc = cursor.next();
	    document.add(doc);
	}
	return document;
    }


    @Override
    public List<Document> findArticleChapters(Map<String,String> params) {
	if(conn==null) {
	    conn = MongoDbFascade.createConnection();
	}
	String bookid = params.get(ArticleConfig.bookid);
	if(bookid==null) {
	    return null;
	}
	List<Bson> linked = new LinkedList<>();
	if(params.get(ArticleConfig.chapter_title)!=null) {
	    String title = params.get(ArticleConfig.chapter_title);
	    linked.add(Filters.eq(ArticleConfig.params[0], title));
	}
	if(params.get(ArticleConfig.paragraph)!=null) {
	    int paragraph = Integer.parseInt(params.get(ArticleConfig.paragraph));
	    linked.add(Filters.eq(ArticleConfig.params[1], paragraph));
	}
	if(linked.size()<1) {
	    return null;
	}
	MongoCursor<Document> cursor = conn.getCollection(bookid)
		.find(Filters.and(linked))
		.iterator();
	List<Document> document = new ArrayList<>();
	while(cursor.hasNext()) {
	    Document doc = cursor.next();
	    document.add(doc);
	}
	return document;
    }
    


}
